/*
 * @Date: 2025-06-18 15:29:20
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-08-25 17:40:52
 * @FilePath: \corpus\src\models\useGlobal.ts
 */
import { getMyViewApply } from '@/services/ant-design-pro/apply';
import { getDeptList } from '@/services/ant-design-pro/dept';
import { useModel } from '@umijs/max';
import { useRequest } from 'ahooks';
import { useEffect, useState } from 'react';
export default () => {
  const [deptInfo, setDeptInfo] = useState({});
  const [deptList, setDeptList] = useState([]);
  const [count, setCount] = useState(0);
  const [chooseDeptList, setChooseDeptList] = useState([]);
  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  const dept = useRequest(
    async () => {
      const res = await getDeptList({});
      if (res) {
        setDeptList(res?.data);
      }
    },
    { manual: true },
  );

  const getDept = useRequest(
    async (arg) => {
      const res = await getDeptList({ parentOrganizationCode: arg });
      setChooseDeptList(res.data);
      // return res;
    },
    { manual: true },
  );
  const getCount = useRequest(
    async (arg) => {
      const res = await getMyViewApply({ current: 1, pageSize: 10 });
      if (res) {
        setCount(res.total);
      }
    },
    { manual: true },
  );

  useEffect(() => {
    if (currentUser) {
      dept.run();
      getCount.run({});

      setInterval(() => {
        getCount.run({});
      }, 1000 * 2 * 60);
    }
  }, [currentUser]);
  return {
    deptInfo,
    setDeptInfo,
    deptList,
    getDept,
    chooseDeptList,
    count,
  };
};
